package com.or.excersise.startwithmatcher;

import java.util.TreeSet;

public class TreePrefixFilter {

    private final TreeSet<String> tree = new TreeSet<String>();   

    public TreePrefixFilter(String[] data) {
        for (int i = 0; i < data.length; i++) {
            tree.add(data[i]);
        }
    }
    
    public String[] match(String prefix) {
        String toElement = getToElement(prefix);
        
        return tree.subSet(prefix, true, toElement, false).toArray(new String[]{});
    }

    static String getToElement(String prefix) {
        StringBuilder sb = new StringBuilder(prefix.substring(0, prefix.length()-1));
        sb.append((char) (prefix.charAt(prefix.length()-1)+1));
        
        String toElement = sb.toString();
        
        return toElement;
    }
     

}
